﻿function communityEditLyric(obj, songId, languageId) {
    $(obj).hide();
    $("#lyric-" + songId + "-" + languageId).attr("readonly", false);
    $("#save-" + songId + "-" + languageId).show();
    $("#cancel-" + songId + "-" + languageId).show();
}

function communitySaveLyric(obj, sId ,lId) {
    $("#lyricRegionSection").hide();
    $("#lyricUpdateIndicator").show();
    var lyricContent = $("#lyric-" + sId + "-" + lId).val();
    $.ajax(
        {
            type: "POST",
            url: "/Song/CommunityUpdateLyric",
            data: { songId: sId, languageId:lId, content :lyricContent },
            success: function (result) {
                if (result.Result) {
                    var cancelObj = document.getElementById("cancel-" + sId + "-" + lId);
                    communityCancelLyric(cancelObj, sId, lId);
                    $("#lyricRegionSection").show();
                    $("#lyricUpdateIndicator").hide();
                    $.msgbox(result.Message, { type: "info" });
                }
                else {
                    $.msgbox(result.Message, { type: "error" });
                }
            }
        });
}

function communityCancelLyric(obj, sId, lId) {
    $("#save-" + sId + "-" + lId).hide();
    $(obj).hide();
    $("#edit-" + sId + "-" + lId).show();
    $("#lyric-" + sId + "-" + lId).attr("readonly", true);
}

function suggestEmbedLinkFunction(sId) {
    $("#loadingIndicatorSuggestEmbedLink").show();
    $("#loadingIndicatorSuggestEmbedLinkSmall").show();
    $("#suggestEmbedLink").hide();
    var embedData = $("#textareaEmbedLink").val();
    if (embedData.length > 0) {
        $.ajax(
            {
                type: "POST",
                url: "/Song/CommunityUpdateEmbedLink",
                data: { songId: sId, content: escape(embedData) },
                success: function (result) {
                    if (result.Result)
                        location.reload();
                    else
                        alert(result.Message);
                }
            });
    }
}

function createNewLanguage(sId) {
    //$("#lyricRegionSection").hide(200);
    //$("#createNewLyricRegion").show(200);
    $("#comboboxLanguage").html("<a href='/Content/Images/loader.gif'> Loading data ...");
    $.ajax(
        {
            type: "POST",
            url: "/Song/CommunityLanguageList",
            data: { songId: sId },
            success: function (result) {
                if (result.Result) {
                    var stringHtml = "<select id='optionLanguageCommunityAdding'>";
                    for (var i = 0; i < result.ReturnObject.length; i++) {
                        stringHtml += "<option value='" + result.ReturnObject[i].LanguageId + "'>" + result.ReturnObject[i].LanguageName + "</option>";
                    }
                    stringHtml += "</select>";
                    $("#comboboxLanguage").html(stringHtml);
                }
                else {
                    alert(result.Message);
                }
            },
            error: function (result) {
                alert();
            }
        });
}

function saveCommunityAddNewLyric(sId) {
    var contentLyric = $("#newLyricContentCommunity").val();
    var lId = $("#optionLanguageCommunityAdding").val();
    if (contentLyric.length > 0) {
        $.ajax(
        {
            type: "POST",
            url: "/Song/CommunityAddMoreLyric",
            data: { songId: sId, languageId: lId, content: contentLyric},
            success: function (result) {
                if (result.Result) {
                    location.reload();
                }
                else {
                    alert(result.Message);
                }
            }
        });
    }
    else {
        alert("It can't be empty.");
    }
}

function cancelCommunityAddNewLyric() {
    $("#lyricRegionSection").show();
    $("#createNewLyricRegion").hide();
}

function reportSong(sId) {
    var checkedValue = $('input[name="report"]:checked').val();
    var reportCommentContent = $("#reportCommentContent").val();
    $("#reportingDiv").hide();
    $("#loadingIndicatorReportSong").show();
    $.ajax(
        {
            type: "POST",
            url: "/Report/ReportSong",
            data: { songId: sId, contentType: checkedValue, content: reportCommentContent },
            success: function (result) {
                if (result.Result) {
                    $("#loadingIndicatorReportSong").hide();
                    $.msgbox(result.Message, { type: "info" });
                }
                else {
                    $("#loadingIndicatorReportSong").hide();
                    $.msgbox(result.Message, { type: "error" });
                }
            }
        });
}

function likeSong(id) {
    $("#functionSongRegion").hide(100);
    $("#functionSongRegionLoading").show(100);
    $.ajax(
        {
            type: "POST",
            url: "/Song/LikeSong",
            data: { songId: id },
            success: function (result) {
                $("#functionSongRegionLoading").hide(100);
                $("#functionSongRegion").show(100);
                $.msgbox(result.Message, { type: "info" });
            }
        });
}

function reportLyric(idL, idS) {
    $.ajax(
        {
            type: "POST",
            url: "/Song/ReportLyric",
            data: { lyricId: idL, songId: idS },
            success: function (result) {
                $.msgbox(result.Message, { type: "info" });
            }
        });
}
function deleteSongs(songId) {
    $.msgbox("Are you sure that you want to permanently delete the selected element?", {
        type: "info",
        buttons: [
          { type: "submit", value: "Yes" },
          { type: "submit", value: "No" },
        ]
    }, function (result) {
        if (result == "Yes") {
            $.ajax(
                {
                    type: "POST",
                    url: "/Song/DeleteSong",
                    data: { id: songId },
                    success: function (result) {
                        if (result.Result) {
                            $.msgbox(result.Message, { type: "info" });
                            location.href = result.ReturnObject;
                        }
                        else {
                            $.msgbox(result.Message, { type: "error" });
                        }
                    }
                });
        }
    });
}

function addFriend(user) {
    $.msgbox("Do you want to add this person as friend?", {
        type: "info",
        buttons: [
          { type: "submit", value: "Yes" },
          { type: "submit", value: "No" },
        ]
    }, function (result) {
        if (result == "Yes") {
            $.ajax(
                {
                    type: "POST",
                    url: "/UserManagement/AddFriend",
                    data: { username: user },
                    success: function (result) {
                        if (result.Result) {
                            $.msgbox(result.Message, { type: "info" });
                        }
                        else {
                            $.msgbox(result.Message, { type: "error" });
                        }
                    }
                });
        }
    });
}